package org.darkforest.merchant.controller;

import org.darkforest.common.services.GiftDistributionService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.EnumParamMapper;
import org.darkforest.dao.mapper.GiftDistributionMapper;
import org.darkforest.model.EnumParam;
import org.darkforest.model.GiftDistribution;
import org.darkforest.vo.StoreSellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * Created by pc on 2017/4/7.
 */
@Controller
@RequestMapping(value="/giftdistribution")
public class GiftDistributionController extends WebController<GiftDistributionController>{
    @Autowired
    private GiftDistributionMapper giftDistributionMapper;
    @Autowired
    private GiftDistributionService giftDistributionService;

    @Autowired
    private EnumParamMapper enumParamMapper;
    @RequestMapping(value="/list")
    public ModelAndView list(GiftDistribution giftDistribution){
        ModelAndView view = new ModelAndView("giftdistribution/list");
        giftDistribution = giftDistributionService.selectByCondition(giftDistribution);
        view.addObject("page",giftDistribution);
        return view;
    }

    @RequestMapping(value="/toadd")
    public ModelAndView toadd(){
        ModelAndView view = new ModelAndView("giftdistribution/add");

        List<EnumParam> enumParamList = enumParamMapper.selectListByCondition(new EnumParam("post"));
        view.addObject("enumParams",enumParamList);
        return view;
    }

    @RequestMapping(value="/doadd")
    public @ResponseBody boolean doadd(HttpServletRequest request,GiftDistribution giftDistribution){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        giftDistribution.setCreaterId(sellerVo.getMemberId());
        giftDistribution.setCreateName(sellerVo.getMemberName());
        giftDistribution.setCreateTime(DateUtil.toTimeString(new Date()));
        giftDistributionMapper.insertSelective(giftDistribution);
        return true;
    }

    @RequestMapping(value="/toedit")
    public ModelAndView toedit(Integer id){
        ModelAndView view = new ModelAndView("giftdistribution/edit");
        GiftDistribution giftDistribution = giftDistributionMapper.selectByPrimaryKey(id);
        view.addObject("giftDistribution",giftDistribution);
        List<EnumParam> enumParamList = enumParamMapper.selectListByCondition(new EnumParam("post"));
        view.addObject("enumParams",enumParamList);
        return view;
    }

    @RequestMapping(value="/doedit")
    public String doedit(HttpServletRequest request,GiftDistribution giftDistribution){
        StoreSellerVo sellerVo= (StoreSellerVo)request.getSession().getAttribute("sellerInfo");
        giftDistribution.setCreaterId(sellerVo.getMemberId());
        giftDistribution.setCreateName(sellerVo.getMemberName());
        giftDistribution.setCreateTime(DateUtil.toTimeString(new Date()));
        giftDistributionMapper.updateByPrimaryKeySelective(giftDistribution);
        return "redirect:list";
    }
}
